<?php
abstract class Db
{
    protected $connection = null;

    abstract protected function disconnect();
    abstract protected function connect();
    abstract protected function isConnected();
    abstract public function query($query);
    abstract protected function getConnectionError();

    protected function __construct()
    {
        $this->connect();
        if (!$this->isConnected())
        {
            Application::throwException('Connection error: ' . $this->getConnectionError());
        }
    }

    public function __destruct()
    {
        if ($this->isConnected())
        {
            $this->disconnect();
        }
    }

    public function config()
    {
        return Application::getDbConfig();
    }

    protected function getConnection()
    {
        return $this->connection;
    }

    public function getPrefix()
    {
        return $this->config()->prefix();
    }
}